package bajw;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.junit.Test;

public class test {
	
//	String urlBase = "http://localhost:8080/bajw";
//	String urlBase = "http://203.86.8.69:84/bajw";
	String urlBase = "http://210.21.231.214:84/bajw";
	
	String sessionId = "1F9069AFD6AA05D83521AF305BC16CC1";
	String uid = "wefadfweueowidjjdcmkdksjcidsfwf";
	String userid = "41";
//	
	@Test
	public void MyTest() throws Exception{
		createCameraHouse();
	}
	
	protected String getData(String url,NameValuePair[] pars,String chartSet) {
		
		HttpClient httpClient=new HttpClient();
		PostMethod postMethod=new PostMethod(url); //设置路径
		postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, chartSet); //设置编码
		postMethod.setRequestBody(pars);   //设置传递参数
		String result=null;
		try {
		   httpClient.executeMethod(postMethod);
		   
		   result=postMethod.getResponseBodyAsString();
		   System.out.println(result);
		} catch (HttpException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}                 
		postMethod.releaseConnection();              //释放连接
		return result;
	}
	
	public void upload(String url,NameValuePair[] pairs, FilePart[] files,String charSet) throws FileUploadException {  
	    PostMethod p = new PostMethod(url);  
        try {  
            //设置param  
            Part[] params = new Part[pairs.length + files.length];  
            for (int i = 0; i < pairs.length; i++) {  
            	String name = pairs[i].getName();
                StringPart stringPart = new StringPart(name,pairs[i].getValue());  
                stringPart.setCharSet(charSet);  
                params[i] = stringPart;  
            }  
            if(files!=null){
            	for (int i = 0; i < files.length; i++) {  
	                FilePart filePart = files[i];  
	  
	                filePart.setCharSet(charSet);  
	                params[pairs.length + i] = filePart;  
	            }  
            }
  
            MultipartRequestEntity post =  new MultipartRequestEntity(params, p.getParams());  
  
            p.setRequestEntity(post);  
  
            HttpClient client = new HttpClient();  
            client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);  
            client.executeMethod(p);  
            System.out.println("--");
            System.out.println(p.getResponseBodyAsString());
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            p.releaseConnection();  
        }  
    }  
	
	public void createCameraHouse() throws FileUploadException, FileNotFoundException{
		NameValuePair userId = new NameValuePair("user_id", "691");
		NameValuePair name = new NameValuePair("name", "数据3");
		NameValuePair lon = new NameValuePair("lon", "1.67527342757676");
		NameValuePair lat = new NameValuePair("lat", "23.352755632527236");
		NameValuePair policeStation = new NameValuePair("policeStation", "1170");
		NameValuePair community = new NameValuePair("community", "5677");
		NameValuePair chargeMan = new NameValuePair("chargeMan", "经度覅交往");
		NameValuePair phone = new NameValuePair("phone", "32435465466");
		NameValuePair cameraNum = new NameValuePair("cameraNum", "3");
		NameValuePair buildingCode = new NameValuePair("buildingCode", "325465436786543");
		NameValuePair cameraNormalNum = new NameValuePair("cameraNormalNum", "23");
		NameValuePair note = new NameValuePair("note", "典籍开书府");
		NameValuePair address = new NameValuePair("address", "地址");
		NameValuePair installDate = new NameValuePair("installDate", "2015-3-13");
		NameValuePair watchManNum = new NameValuePair("watchManNum", "22");
		NameValuePair uuid = new NameValuePair("uuid", "232");
		
		FilePart par1 = new FilePart("img1",new File("d:/2.png"));
		FilePart par2 = new FilePart("img2",new File("d:/2.png"));
		FilePart par3 = new FilePart("img3",new File("d:/2.png"));
		
		String url = urlBase + "/CameraHouse/createApp.do";
		upload(url, new NameValuePair[]{uuid,installDate,watchManNum,cameraNormalNum,address,userId, name, lat, lon, policeStation, community, chargeMan, phone, cameraNum, buildingCode, note}, 
				new FilePart[]{}, "utf-8");
	}
	
	public void applog() throws FileUploadException, FileNotFoundException{
		NameValuePair userId = new NameValuePair("user_id", "691");
		NameValuePair version = new NameValuePair("version", "version11");
		NameValuePair imei = new NameValuePair("imei", "imeie");
		NameValuePair os = new NameValuePair("os", "os3");
		NameValuePair vendor = new NameValuePair("vendor", "vendor");
		NameValuePair model = new NameValuePair("model", "model");
		NameValuePair cpu = new NameValuePair("cpu", "cpu");
		NameValuePair log = new NameValuePair("log", "崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦"
				+ "崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃"
				+ "啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦崩溃啦");
		
		String url = urlBase + "/AppLog/add.do";
		upload(url, new NameValuePair[]{userId,version,imei,os,log,vendor,model,cpu}, 
				new FilePart[]{}, "utf-8");
	}
	
	public void register() throws FileUploadException, FileNotFoundException{
		NameValuePair username = new NameValuePair("username", "kingerpk3");
		NameValuePair password = new NameValuePair("password", "123456");
		NameValuePair telNumber = new NameValuePair("telNumber", "5584253180");
		NameValuePair sex = new NameValuePair("sex", "男");
		NameValuePair showname = new NameValuePair("showname", "谭林");
		NameValuePair cardId = new NameValuePair("cardId", "5677");
		NameValuePair bankcardNo = new NameValuePair("bankcardNo", "bankcardNo");
		NameValuePair usertype = new NameValuePair("usertype", "5");
		NameValuePair mjjh = new NameValuePair("mjjh", "3");
		NameValuePair pcs = new NameValuePair("pcs", "3");
		NameValuePair jq = new NameValuePair("jq", "4");
		NameValuePair zrq = new NameValuePair("zrq", "5");
		NameValuePair tuijianren = new NameValuePair("tuijianren", "kk");
		
		String url = urlBase + "/User/register.do";
		upload(url, new NameValuePair[]{username,password,telNumber,sex,showname,cardId,bankcardNo,usertype,mjjh,pcs,jq,zrq,tuijianren}, 
				new FilePart[]{}, "utf-8");
	}
	
	public void getUserTeam(){
		NameValuePair userId = new NameValuePair("user_id", "691");
		String url = urlBase + "/User/getUserTeam.do";
		getData(url, new NameValuePair[]{userId}, "utf-8");
	}
	
	public void createCamera() throws FileNotFoundException, FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", "691");
		NameValuePair lon = new NameValuePair("lat", "22.54122222222222222222222222222222222");
		NameValuePair lat = new NameValuePair("lon", "111.526132222232222232222232222222222222");
		NameValuePair belongName = new NameValuePair("belongName", "回答我");
		NameValuePair belongPhone = new NameValuePair("belongPhone", "41264361651");
		NameValuePair saveTime = new NameValuePair("saveTime", "大货");
		NameValuePair houseAddress = new NameValuePair("houseAddress", "和贷款环节的");
		NameValuePair factoryName = new NameValuePair("factoryName", "氮氧化物iu");
		NameValuePair cameraType = new NameValuePair("cameraType", "2");
		NameValuePair cameraDirection = new NameValuePair("cameraDirection", "偶记");
		NameValuePair cameraClass = new NameValuePair("cameraClass", "点击");
		NameValuePair cameraAddress = new NameValuePair("cameraAddress", "大行情我肯定会");
		NameValuePair atmCode = new NameValuePair("atmCode", "426574362826");
		NameValuePair baseStationBelong = new NameValuePair("baseStationBelong", "春节晚会丢我");
		NameValuePair baseStationName = new NameValuePair("baseStationName", "客户外科");
		NameValuePair baseStationCode = new NameValuePair("baseStationCode", "63274623927");
		NameValuePair creater = new NameValuePair("creater", "41");
		NameValuePair cameraPlace = new NameValuePair("cameraPlace", "加快文化");
		NameValuePair buildingCode = new NameValuePair("buildingCode", "6238723652637687");
		NameValuePair note = new NameValuePair("note", "出口计划为客户");
		NameValuePair installDate = new NameValuePair("installDate", "2015-3-13");
		NameValuePair indoorOutdoor = new NameValuePair("indoorOutdoor", "1");
		NameValuePair uuid = new NameValuePair("uuid", "232");
		
		FilePart par1 = new FilePart("img1",new File("d:/2.png"));
		FilePart par2 = new FilePart("img2",new File("d:/2.png"));
		FilePart par3 = new FilePart("img3",new File("d:/2.png"));
		
		String url = urlBase + "/Camera/createApp.do";
		upload(url, new NameValuePair[]{uuid,userId, atmCode, baseStationBelong, baseStationCode, baseStationName, belongName,
				belongPhone, cameraAddress, cameraClass, cameraDirection, cameraType, creater, 
				factoryName, houseAddress, lat, lon, saveTime, cameraPlace, buildingCode, note,installDate,indoorOutdoor}, 
				new FilePart[]{}, "utf-8");
	}
	
	public void getAllScore(){
		NameValuePair userId = new NameValuePair("user_id", "686");
		NameValuePair date = new NameValuePair("date", "2015-2-2");
		String url = urlBase + "/PerformanceTModel/getPerformanceListApp.do";
		getData(url, new NameValuePair[]{userId,date}, "utf-8");
	}
	
	public void UserTeam(){
		NameValuePair userId = new NameValuePair("user_id", "902");
		String url = urlBase + "/User/getUserTeam.do";
		getData(url, new NameValuePair[]{userId}, "utf-8");
	}
	
	public void postAlarm() throws FileNotFoundException, FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", "701");
		NameValuePair id = new NameValuePair("createBy", uid);
		NameValuePair mapx = new NameValuePair("mapx", "113.12");
		NameValuePair mapy = new NameValuePair("mapy", "23.21");
		NameValuePair addr = new NameValuePair("addr", "南山区科技园");
		NameValuePair buildingCode = new NameValuePair("content", "1234567890123456789");
		
		FilePart par1 = new FilePart("img1",new File("d:/2.png"));
		FilePart par2 = new FilePart("img2",new File("d:/2.png"));
		FilePart par3 = new FilePart("img3",new File("d:/2.png"));
		FilePart par4 = new FilePart("voice",new File("d:/2.png"));
		
		String url=urlBase+"/PrizeAlarm/insert.do?HttpSessionId="+sessionId;
		upload(url,
				new NameValuePair[]{userId,id,mapx,mapy,addr,buildingCode},
				new FilePart[]{par1,par2,par3,par4},
				"utf-8");
	}
	
	public void baidu(){
		NameValuePair userId = new NameValuePair("user_id", "701");
		NameValuePair baiduUserid= new NameValuePair("baidu_userid", "679668");
		NameValuePair baiduChannelid= new NameValuePair("baidu_channelid", "hdwhihwduhwuyhdiwhi");
		String url = urlBase + "/User/updateMsgPushInfo.do";
		getData(url, new NameValuePair[]{userId,baiduUserid,baiduChannelid}, "utf-8");
	}
	
	public void build(){
		NameValuePair userId = new NameValuePair("user_id", "11");
		NameValuePair id= new NameValuePair("id", "679668");
		String url = urlBase + "/buildingModel/getBuildingModel.do";
		getData(url, new NameValuePair[]{userId,id}, "utf-8");
	}
	
	public void userInfomation(){
		NameValuePair userId = new NameValuePair("user_id", "11");
		String url = urlBase + "/User/useDetailsApp.do";
		getData(url, new NameValuePair[]{userId}, "utf-8");
	}
	
	public void postPosition(int i) throws FileNotFoundException, FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", ""+i);
		NameValuePair test = new NameValuePair("test", "41");
		NameValuePair position_x = new NameValuePair("position_x", "1");
		NameValuePair position_y = new NameValuePair("position_y", "2");
		String url = urlBase + "/User/postLocation.do";
		getData(url, new NameValuePair[]{userId,position_x,position_y,test}, "utf-8");
	}
	
	public void getUserPerformanceById() throws FileNotFoundException, FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", "41");
		String url = urlBase + "/PerformanceTModel/getUserPerformanceById.do";
		upload(url, new NameValuePair[]{userId}, new FilePart[]{}, "utf-8");
	}
	
	public void uploadUserImage() throws FileNotFoundException, FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", "701");
		FilePart img = new FilePart("img", new File("D:/2.png"));
		String url = urlBase + "/User/updateImageUrl.do";
		upload(url, new NameValuePair[]{userId}, new FilePart[]{img}, "utf-8");
	}
	
	public void login() throws FileNotFoundException, FileUploadException{
		NameValuePair username = new NameValuePair("username", "13509608859");
		NameValuePair password = new NameValuePair("password", "123456");
		
		String url=urlBase+"/User/Login.do";
		getData(url,new NameValuePair[]{username,password},"utf-8");
	}
	
	public void postData() throws FileNotFoundException, FileUploadException{
		NameValuePair id = new NameValuePair("id", uid);
		NameValuePair userId = new NameValuePair("user_id", "41");
		NameValuePair mapx = new NameValuePair("mapx", "113.12");
		NameValuePair mapy = new NameValuePair("mapy", "23.21");
		NameValuePair addr = new NameValuePair("addr", "南山区科技园");
		NameValuePair buildingCode = new NameValuePair("buildingCode", "1234567890123456789");
		NameValuePair buildingType = new NameValuePair("buildingType", "2");
		NameValuePair infoType = new NameValuePair("infoType", "1");
		NameValuePair telno = new NameValuePair("telno", "18038033894");
		NameValuePair note = new NameValuePair("note", "notetete");
		
		FilePart par3 = new FilePart("img",new File("d:/2.png"));
		FilePart par4 = new FilePart("record",new File("d:/2.png"));
		
		String url=urlBase+"/Termpost/post.do?HttpSessionId="+sessionId;
		upload(url,
				new NameValuePair[]{userId,id,mapx,mapy,addr,buildingCode,buildingType,telno,note,infoType},
				new FilePart[]{par3,par4},
				"utf-8");
	}
	
	
	///////////////////////////////指令接口测试
	public void getOrder() throws FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", "269");
		NameValuePair state = new NameValuePair("isfinish", "0");
		NameValuePair pageIndex = new NameValuePair("grid_pageIndex", "1");
		NameValuePair pageSize = new NameValuePair("grid_pageSize", "3");
		String url = urlBase + "/OrderPushTModel/getOrderModelList.do";
		System.out.println(url);
		upload(url, new NameValuePair[]{userId, state, pageIndex, pageSize},new FilePart[]{},"utf-8");
	}
	
	public void getOrderModel() throws FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", "13823356766");
		NameValuePair id = new NameValuePair("id", "22");
		String url = urlBase + "/OrderPushTModel/getOrderPushTModel.do";
		upload(url, new NameValuePair[]{id,userId}, new FilePart[]{}, "utf-8");
	}
	
	public void getOrderData() throws FileNotFoundException, FileUploadException{
		NameValuePair id = new NameValuePair("id", "66");
		NameValuePair userid = new NameValuePair("user_id", "18566743550");
		NameValuePair positionX = new NameValuePair("position_x", "111.2324234321");
		NameValuePair positionY = new NameValuePair("position_y", "111.4234321423");
		NameValuePair address = new NameValuePair("address", "广东省深圳市宝安区裕安一路3020号");
		NameValuePair note = new NameValuePair("note", "任务已完成");

		FilePart par3 = new FilePart("img", new File("d:/2.bmp"));
		FilePart par4 = new FilePart("record", new File("d:/2.png"));
		
		String url = urlBase + "/OrderPushTModel/CompleteInstructions.do";
		
		upload(url, new NameValuePair[]{id, userid, positionX, positionY, note, address},
				new FilePart[]{par3, par4}, "utf-8");
	}
	
	public void getModel() throws FileUploadException{
		NameValuePair id = new NameValuePair("id", "1");
		NameValuePair userid = new NameValuePair("user_id", "41");
		String url = urlBase + "/OrderPushTModel/getOrderPushTModel.do";
		System.out.println(url);
		upload(url, new NameValuePair[]{id,userid},new FilePart[]{},"utf-8");
	}
	
	/////////////////////////////////////////////////////////////////////////////////
	
	public void test2() throws FileUploadException{
		NameValuePair id = new NameValuePair("id", "766661");
		NameValuePair userId = new NameValuePair("user_id", "41");
		String url = urlBase + "/buildingModel/getModel1.do";
		upload(url, new NameValuePair[]{id,userId}, new FilePart[]{}, "utf-8");
	}
	
	////////////////////////////////采集数据查询列表接口测试
	public void list() throws FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", "688");
		NameValuePair cType = new NameValuePair("coll_type", "");
		NameValuePair bType = new NameValuePair("building_type", "");
		NameValuePair bCode = new NameValuePair("building_code", "");
		NameValuePair stateDate = new NameValuePair("start_date", "");
		NameValuePair stopDate = new NameValuePair("stop_date", "");
		String url = urlBase + "/Termpost/list.do";
		upload(url, new NameValuePair[]{userId,bCode,cType,bType,stateDate,stopDate},
				new FilePart[]{}, "utf-8");
	}
	
	public void getTermpostModel() throws FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", "688");
		NameValuePair id = new NameValuePair("id", "766698");
		String url = urlBase + "/Termpost/getTermpostModel.do";
		upload(url, new NameValuePair[]{id,userId},
				new FilePart[]{}, "utf-8");
	}
	
	public void getFuzzySearch() throws FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", "688");
		NameValuePair cons = new NameValuePair("cons", "");
		NameValuePair pageIndex = new NameValuePair("grid_pageIndex", "1");
		NameValuePair pageSize = new NameValuePair("grid_pageSize", "10");
		String url = urlBase + "/Termpost/fuzzySearch.do";
		upload(url, new NameValuePair[]{userId, cons, pageIndex, pageSize}, new FilePart[]{}, "utf-8");
	}
	
	
	///////////////////////////////////签到接口测试
	
	public void Sign() throws FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", "41");
		NameValuePair addr = new NameValuePair("address", "南山区沙河路");
		NameValuePair positionX = new NameValuePair("position_x", "111.111");
		NameValuePair positionY = new NameValuePair("position_y", "111.111");
		String url = urlBase + "/SignTModel/sign.do";
		upload(url, new NameValuePair[]{userId, addr, positionX, positionY},
				new FilePart[]{}, "utf-8");
	}
	
	public void querySignHistory() throws FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", "41");
		NameValuePair startDate = new NameValuePair("start_date", "2014-12-01");
		NameValuePair stopDate = new NameValuePair("stop_date", "2014-12-31");
		String url = urlBase + "/SignTModel/querySignHistory.do";
		upload(url, new NameValuePair[]{userId, startDate, stopDate},
				new FilePart[]{}, "utf-8");
	}
	
	public void querySignHistoryByOneDay() throws FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", "41");
		NameValuePair date = new NameValuePair("date", "");
		String url = urlBase + "/SignTModel/querySignHistoryByOneDay.do";
		upload(url, new NameValuePair[]{userId, date},
				new FilePart[]{}, "utf-8");
	}
	
	public void deleteModel() throws FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", "41");
		NameValuePair id = new NameValuePair("id", "29");
		String url = urlBase + "/SignTModel/deleteSign.do";
		upload(url, new NameValuePair[]{userId, id},
				new FilePart[]{}, "utf-8");
	}
	
	//////////////////////////////位置信息上传
	
	public void location() throws FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", "1");
		NameValuePair positionX = new NameValuePair("position_x", "113.900232");
		NameValuePair positionY = new NameValuePair("position_y", "22.555255");
		String url = urlBase + "/User/postLocation.do";
		upload(url, new NameValuePair[]{userId,positionX,positionY}, new FilePart[]{}, "utf-8");
	}
	
	public void version() throws FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", "41");
		NameValuePair Vno = new NameValuePair("version", "V2.1");
		NameValuePair appName = new NameValuePair("app_name", "bajw");
		String url = urlBase + "/AppVersionTModel/checkVersion.do";
		upload(url, new NameValuePair[]{userId, Vno, appName}, new FilePart[]{}, "utf-8");
	}
	
	public void getList() throws FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", "41");
		String url = urlBase + "/Todo/getList.do";
		upload(url, new NameValuePair[]{userId}, new FilePart[]{}, "utf-8");
	}
	
	public void getPerformance() throws FileUploadException{
		NameValuePair userId = new NameValuePair("user_id", "41");
		String url = urlBase + "/PerformanceTModel/getPerformanceTModelList.do";
		upload(url, new NameValuePair[]{userId}, new FilePart[]{}, "utf-8");
	}
	
	public void finishDubanTodo() throws FileUploadException, FileNotFoundException{
		NameValuePair userId = new NameValuePair("user_id", "948");
		NameValuePair todoId = new NameValuePair("todo_id", "260");
		NameValuePair finishX = new NameValuePair("x", "1.1");
		NameValuePair finishY = new NameValuePair("y", "2.2");
		NameValuePair note = new NameValuePair("note", "我是备注备注备注备注我是备注备注备注备注我是备注备注备注备注");
		FilePart par3 = new FilePart("img",new File("d:/1.png"));
		String url = urlBase + "/Todo/finish.do";
		FilePart voice = new FilePart("voice",new File("d:/1.png"));
		upload(url, new NameValuePair[]{userId,todoId,finishX,finishY,note}, new FilePart[]{par3}, "utf-8");
	}
}
